﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NewsPublish.Model.Request;
using NewsPublish.Model.Response;
using NewsPublish.Service;
using Microsoft.AspNetCore.Hosting;
using System.IO;

namespace NewsPublish.Web.Areas.Admin.Controllers
{
    [Area("Admin")]
    public class BannerController : Controller
    {
        private BannerService _bannerService;
        private IHostingEnvironment _host;

        /// <summary>
        /// 构造函数 初始化变量
        /// </summary>
        /// <param name="bannerService"></param>
        public BannerController(BannerService bannerService, IHostingEnvironment host)
        {
            this._bannerService = bannerService;
            this._host = host;
        }

        /// <summary>
        /// Index方法
        /// </summary>
        /// <returns>banner列表页面</returns>
        public ActionResult Index()
        {
            var banner = _bannerService.GetBannerList();
            return View(banner);
        }

        /// <summary>
        /// 显示banner新增页面
        /// </summary>
        /// <returns>banner新增页面</returns>
        public ActionResult BannerAdd()
        {
            return View();
        }

        /// <summary>
        /// 添加banner
        /// </summary>
        /// <param name="banner">banner新增对象</param>
        /// <param name="collection">图片参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> AddBanner(AddBanner banner, IFormCollection collection)
        {
            var files = collection.Files;
            if (files.Count > 0)
            {
                var webRootPath = _host.WebRootPath;//获取wwwroot的路径
                string relativeDirPath = "\\BannerPic"; //相对路径
                string absolutePath = webRootPath + relativeDirPath; //图片文件保存绝对路径

                string[] fileTypes = new string[] { ".gif", ".jpg", ".jpeg", ".png", ".bmp" }; //允许上传的图片类型
                string extension = Path.GetExtension(files[0].FileName); //图片文件后缀名

                if (fileTypes.Contains(extension.ToLower()))
                {
                    //文件路径不存在则创建
                    if (!Directory.Exists(absolutePath))
                    {
                        Directory.CreateDirectory(absolutePath);
                    }

                    string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + extension;//自定义文件名称
                    var filePath = absolutePath + "\\" + fileName;//文件保存完整路径

                    //将图片文件保存到指定目录中
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await files[0].CopyToAsync(stream);
                    }

                    banner.Image = "/BannerPic/" + fileName;
                    return Json(_bannerService.AddBnaner(banner));//将页面输入的信息保存到数据库
                }
                else
                {
                    return Json(new ResponseModel { code = 0, result = "图片格式有误" });
                }
            }
            else
            {
                return Json(new ResponseModel { code = 0, result = "请上传图片文件" });
            }
        }

        /// <summary>
        /// 删除一个Banner
        /// </summary>
        /// <param name="bannerID">Banner的ID</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult DelBanner(int bannerID)
        {
            if (bannerID <= 0)
            {
                return Json(new ResponseModel { code = 0, result = "参数有误,Banner删除失败" });
            }

            return Json(_bannerService.DeleteBanner(bannerID));
        }


    }
}
